package cn.jbit.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jbit.news.dao.impl.NewsDaoImpl;
import org.jbit.news.entity.News;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

@SuppressWarnings("serial")
public class ajaxShowNews extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out =resp.getWriter();
    	NewsDaoImpl nd=new NewsDaoImpl();
		List<News> list=nd.getAllnews();
		String strJSON=JSON.toJSONString(list,
				SerializerFeature.WriteMapNullValue,
				SerializerFeature.WriteNullNumberAsZero,
				SerializerFeature.WriteNullStringAsEmpty);
		out.println(strJSON);
		out.flush();
		out.close();
		//[{"news":"value","news":"value"},{},{}]
//		StringBuffer json = new StringBuffer("[");
//		for(int i=0;i<list.size();i++){
//			News news =list.get(i);
//			String newsStr = "{\"ntitle\":\""+news.getNtitle().replace("\"", "")+"\",\"nauthor\":\""+news.getNauthor()+"\",\"nid\":\""+news.getNid()+"\"}";
//			json.append(newsStr);
//			if(i<list.size()-1){
//					json.append(",");
//			}
//		}
//		json.append("]");
//		out.println(json);
//		out.flush();
//		out.close();
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	}

}
